package nac.myclinic.entity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "client")
public class Client implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String firstName;
  private String lastName;
  private String middleName;
  @Temporal(TemporalType.DATE)
  private Date birthdate;
  @Enumerated(EnumType.STRING)
  private Sex sex;
  private boolean deleted;
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "client")
  private Collection<Visit> visits;
  @Lob
  @Column(length = 512)
  private String pastMedicalHistory;
  @Lob
  @Column(length = 512)
  private String pastSurgicalHistory;
  @Lob
  @Column(length = 512)
  private String socialHistory;
  @Lob
  @Column(length = 512)
  private String familyHistory;
  private String allergies;
  private String contactInfo;

  public String getFamilyHistory() {
    return familyHistory;
  }

  public void setFamilyHistory(String familyHistory) {
    this.familyHistory = familyHistory;
  }

  public String getPastSurgicalHistory() {
    return pastSurgicalHistory;
  }

  public void setPastSurgicalHistory(String pastSurgicalHistory) {
    this.pastSurgicalHistory = pastSurgicalHistory;
  }

  public String getSocialHistory() {
    return socialHistory;
  }

  public void setSocialHistory(String socialHistory) {
    this.socialHistory = socialHistory;
  }

  public String getContactInfo() {
    return contactInfo;
  }

  public void setContactInfo(String contactInfo) {
    this.contactInfo = contactInfo;
  }

  public String getPastMedicalHistory() {
    return pastMedicalHistory;
  }

  public void setPastMedicalHistory(String pastMedicalHistory) {
    this.pastMedicalHistory = pastMedicalHistory;
  }

  public String getAllergies() {
    return allergies;
  }

  public void setAllergies(String allergies) {
    this.allergies = allergies;
  }

  public Collection<Visit> getVisits() {
    return visits;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getMiddleName() {
    return middleName;
  }

  public void setMiddleName(String middleName) {
    this.middleName = middleName;
  }

  public Date getBirthdate() {
    return birthdate;
  }

  public String getBirthdate(String format) {
    return new SimpleDateFormat(format).format(birthdate);
  }

  public int getAge() {
    Calendar now = Calendar.getInstance();
    Calendar dob = Calendar.getInstance();
    dob.setTime(birthdate);

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);
    int age = year1 - year2;
    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);
    if (month2 > month1) {
      age--;
    } else if (month1 == month2) {
      int day1 = now.get(Calendar.DAY_OF_MONTH);
      int day2 = dob.get(Calendar.DAY_OF_MONTH);
      if (day2 > day1) {
        age--;
      }
    }
    return age;
  }

  public void setBirthdate(Date birthdate) {
    this.birthdate = birthdate;
  }

  public boolean isDeleted() {
    return deleted;
  }

  public void setDeleted(boolean deleted) {
    this.deleted = deleted;
  }

  public Sex getSex() {
    return sex;
  }

  public void setSex(Sex sex) {
    this.sex = sex;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {

    if (!(object instanceof Client)) {
      return false;
    }
    Client other = (Client) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "com.myweb.entity.Client[ id=" + id + " ]";
  }
}
